﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ninject;
using Mabalacat.CPS.BusinessLogic.Services.Interfaces;
using Web.Codes.Providers;
using System.Web.Security;

namespace Web
{
    public partial class Login : System.Web.UI.Page
    {
        [Inject]
        public IUserService _userService { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {            
        }

        protected void Loginbtn_Click(object sender, EventArgs e)
        {
            Session.Remove("Membership");              
            var userName = UsernameTxt.Value;
            var passWord = PasswordTxt.Value;
            
            CustomMembershipProvider membershipProvider = (CustomMembershipProvider)Membership.Provider;
            var valid = membershipProvider.ValidateUser(userName, passWord);
            if (valid)
            {
                FormsAuthentication.SetAuthCookie(userName, true);
                var redireUrl = FormsAuthentication.GetRedirectUrl(userName, true);
                if (!string.IsNullOrEmpty(redireUrl) && !redireUrl.Contains("/default"))
                    FormsAuthentication.RedirectFromLoginPage(userName, true);
                else
                    Response.Redirect(ResolveUrl("~/Admin/UserEntry.aspx"));
            }                
        }
    }
}